package oss

import (
	"fmt"
	"io"
	"mime/multipart"
	"time"

	"admin.supervise.limaochuxing.top/util"
)

type OssManager struct {
    driver OSS
}

type Option func(*OssManager)

func WithDriver(driver OSS) Option {
    return func(s *OssManager) {
        s.driver = driver
    }
}

func NewOssManager(options ...Option) *OssManager {
    manager:=&OssManager{}
    for _,option:=range options{
        option(manager)
    }
    if manager.driver==nil{
        manager.driver=NewAliyunService()
    }
    return manager
}

func (s *OssManager) UploadFile(file *multipart.FileHeader,filePath string) (string,error) {
    url,err:= s.driver.UploadFile(file,filePath)
    fmt.Println(url,url)
    if err!=nil{
        return "",err
    }
    return url,nil
}

// GenerateFileName 生成带随机字符串的文件名
func GenerateFileName(ext string) string {
	return time.Now().Format("150405") + "_" + util.ToolsUtil.RandomString(6) + ext
}

// PutObject 通用上传方法
func (om *OssManager) PutObject(objectKey string, file io.Reader) (string, error) {
	url,err := om.driver.PutObject(objectKey, file)
	if err != nil {
		return "", err
	}
	return url, nil
}